# -*- coding: utf-8 -*-
from django.core.serializers.json import DjangoJSONEncoder
from django.http import HttpResponse

import json


def response(is_success=True, data=None, success='', error=''):
    if is_success:
        return success_response(data, success)
    else:
        return error_response(error)


def success_response(data=None, message='SUCCESS'):
    result = {
        'errcode': 0,
        'message': message,
    }
    if data is not None:
        result['data'] = data
    return HttpResponse(json.dumps(result, cls=DjangoJSONEncoder), content_type='application/json')


def error_response(message='', errcode=1):
    result = {
        'errcode': errcode,
        'message': message,
    }
    return HttpResponse(json.dumps(result, cls=DjangoJSONEncoder), content_type='application/json')


def error(err):
    result = {
        'errcode': err['id'],
        'message': err['message'],
    }
    return HttpResponse(json.dumps(result, cls=DjangoJSONEncoder), content_type='application/json')


# --------------------------------------------------------------------------------------------------
if __name__ == '__main__':
    print('ok')
